﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using SAC.Helper;
using DAL;
using System.Web;

namespace BLL
{
    public class BLLEeds
    {
        static string xzTime = "2012-1-1";     //取值限制时间
        static string toExcel = "yse";         //导出Excel是否控制小数点
        string rtDBType = "";   //实时数据库
        string rlDBType = "";   //关系数据库

        ArrayList list = new ArrayList();
        StrHelper sh = new StrHelper();
        StringBuilder sb = new StringBuilder();

        private void init()
        {
            //JSQC = IniHelper.ReadIniData("Report", "JSQC", null);
            rtDBType = IniHelper.ReadIniData("RTDB", "DBType", null);
            rlDBType = IniHelper.ReadIniData("RelationDBbase", "DBType", null);
        }

        #region 数据录入
        public ArrayList SelectSGLRDAY(string date, string[] point, out string errMsg)
        {
            return DAL.DALEeds.RetSGLRDAY(date, point, out errMsg);
        }

        public bool SaveSGLRDAY(string[] sapoint, string[] val, string date, out string errMsg)
        {
            return DAL.DALEeds.SaveSGLRDAY(sapoint, val, date, out errMsg);
        }

        public ArrayList SelectSGLRBZ(string date, string[] point, string BZ, out string errMsg)
        {
            return DAL.DALEeds.RetSGLRBZ(date, point, BZ, out errMsg);
        }

        public bool SaveSGLRBZ(string[] arrpoint, string[] val, string date, string BZ, out string errMsg)
        {
            return DAL.DALEeds.SaveSGLRBZ(arrpoint, val, date, BZ, out errMsg);
        }

        public string SelectBZ(string TimeBZ, out string errMsg)
        {
            return DAL.DALEeds.RetBZ(TimeBZ, out errMsg);
        }
        #endregion

        #region 全厂日报&月报

        /// <summary>
        /// 日报(机组轮转)
        /// </summary>
        /// <param name="repName"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public string RetTableByDay(string repName, string repType, DateTime date)
        {
            string[] arr;
            string qsrq;
            string jsrq;
         
            bool isTure = false;

            DateTime dtNow = DateTime.Now;

            this.init();

            if (date < dtNow && date >= Convert.ToDateTime(xzTime))
            {
                if (dtNow.Year == date.Year && dtNow.Month == date.Month && dtNow.Day == date.Day)
                {
                    qsrq = date.ToString("yyyy-MM-dd 00:00:00");
                    jsrq = dtNow.AddHours(-1).ToString("yyyy-MM-dd H:00:00");
                    isTure = true;
                }
                else
                {
                    qsrq = date.ToString("yyyy-MM-dd H:mm:ss");
                    jsrq = date.AddDays(+1).ToString("yyyy-MM-dd H:mm:ss");
                }
            }
            else
            { sb.Append("请输入正确的日期进行查询......"); return sb.ToString(); }

            if (repType == "日报")
            { repType = "日点"; }

            DALReport drp = new DALReport();

            list = drp.RetArrsRepDay(repName, repType, qsrq, jsrq, isTure);

            string[] zb = (string[])list[0];
            string[] un = (string[])list[1];
            string[] lay = (string[])list[2];
            string[] dv = (string[])list[3];
            string[] dvExcel = (string[])list[4];

            if (list.Count > 0)
            {
                if (toExcel == "yes")
                    sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX\">");
                else
                    sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX1\">");

                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单 位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">设计值</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">全厂</td>");
                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat("<td align='center'>{0}</td>", zb[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", un[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", lay[i]);

                    if (dv[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dv[i].TrimEnd(',').ToString());

                    sb.Append("</tr>");
                }

                sb.Append("</table>");

                if (toExcel == "no")
                    sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX\" style=\"display:none;\">");
                else
                    sb.Append("<table width=\"90%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX1\" style=\"display:none;\">");

                sb.Append("<tr style=\"width: 80%;\" bgcolor=\"#CFE6FC\"> ");
                sb.Append("<td style=\"width: 20%;\"><div align=\"center\">指 标 名 称</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">单 位</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">设计值</td>");
                sb.Append("<td style=\"width: 10%;\"><div align=\"center\">全厂</td>");
                sb.Append("</tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat("<td align='center'>{0}</td>", zb[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", un[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", lay[i]);

                    if (dvExcel[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dvExcel[i].TrimEnd(',').ToString());

                    sb.Append("</tr>");
                }

                sb.Append("</table>");

            }
            else
            { sb.Append("没有查询到结果..."); return sb.ToString(); }

            return sb.ToString();
        }

        /// <summary>
        /// 月报/年报(机组轮转)
        /// </summary>
        /// <param name="repName"></param>
        /// <param name="repType"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public string RetTableByMonth(string repName, string repType, DateTime date)
        {
            bool isTure = false;

            string[] arr;
            string pType = "";
            string qsrqB = "";
            string jsrqB = "";
            string qsrqT = "";
            string jsrqT = "";

            DateTime ndate = DateTime.Now.Date;

            if (date < Convert.ToDateTime(xzTime) || date > ndate)
            { sb.Append("请输入正确日期查询"); return sb.ToString(); }

            if (repType == "月报")
            {
                pType = "月点";

                if (date.Year == ndate.Year && date.Month == ndate.Month)
                {
                    qsrqB = date.ToString("yyyy-MM-dd H:mm:ss");
                    //jsrqB = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd H:00:00");
                    jsrqB = DateTime.Now.ToString("yyyy-MM-dd 0:00:00");

                    qsrqT = DateTime.Parse(qsrqB).AddYears(-1).ToString("yyyy-MM-dd H:mm:ss");
                    jsrqT = DateTime.Parse(qsrqB).AddYears(-1).AddMonths(+1).ToString("yyyy-MM-dd H:mm:ss");

                    isTure = true;
                }
                else
                {
                    qsrqB = date.ToString("yyyy-MM-dd H:mm:ss");
                    jsrqB = sh.LastDayOfMonth(DateTime.Parse(qsrqB)).AddDays(+1).ToString("yyyy-MM-dd H:mm:ss");

                    qsrqT = DateTime.Parse(qsrqB).AddYears(-1).ToString("yyyy-MM-dd H:mm:ss");
                    jsrqT = DateTime.Parse(jsrqB).AddYears(-1).ToString("yyyy-MM-dd H:mm:ss");
                }
            }
            else if (repType == "年报")
            {
                pType = "年点";

                if (date.Year == ndate.Year)
                {
                    qsrqB = date.ToString("yyyy-MM-dd H:mm:ss");
                    //jsrqB = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd H:00:00");
                    jsrqB = DateTime.Now.ToString("yyyy-MM-dd 0:00:00");

                    qsrqT = DateTime.Parse(qsrqB).AddYears(-1).ToString("yyyy-MM-dd H:mm:ss");
                    jsrqT = qsrqB;

                    isTure = true;
                }
                else
                {
                    qsrqB = date.ToString("yyyy-MM-dd H:mm:ss");
                    jsrqB = DateTime.Parse(qsrqB).AddYears(+1).ToString("yyyy-MM-dd H:mm:ss");

                    qsrqT = DateTime.Parse(qsrqB).AddYears(-1).ToString("yyyy-MM-dd H:mm:ss");
                    jsrqT = qsrqB;
                }
            }

            DALReport drp = new DALReport();

            list = drp.RetArrsRepMonth(repName, repType, pType, qsrqB, jsrqB, qsrqT, jsrqT, isTure);

            string[] zb = (string[])list[0];
            string[] un = (string[])list[1];
            string[] lay = (string[])list[2];
            string[] dv = (string[])list[3];
            string[] dvT = (string[])list[4];
            string[] dvExcel = (string[])list[5];
            string[] dvExcelT = (string[])list[6];

            if (list.Count > 0)
            {
                sb.Append("<table width=\"98%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX1\">");
                sb.Append("<tr bgcolor=\"#CFE6FC\"> <td rowspan=\"2\" align=\"center\"> 指 标 名 称  </td>");
                sb.Append("<td rowspan=\"2\" align=\"center\">单 位</td>");
                sb.Append("<td rowspan=\"2\" align=\"center\">设 计 值</td>");
                sb.Append("<td colspan=\"1\" align=\"center\">本 期</td>");
                sb.Append("<td colspan=\"1\" align=\"center\">同 期</td></tr>");
                sb.Append("<tr bgcolor=\"#CFE6FC\">");
                sb.Append("<td align=\"center\">全厂</td>");
                sb.Append("<td align=\"center\">全厂</td></tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat("<td align='center'>{0}</td>", zb[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", un[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", lay[i]);

                    if (dv[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dv[i].TrimEnd(','));
                    else
                        sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");

                    if (dvT[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dvT[i].TrimEnd(','));
                    else
                        sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");

                    sb.Append("</tr>");
                }

                sb.Append("</table>");

                #region 导出 Excel 表

                sb.Append("<table width=\"98%\" border=\"1\" align=\"center\" class=\"bg\" valign=\"top\" bordercolorlight=\"#999999\" bordercolordark=\"#FFFFFF\" cellspacing=\"0\" cellpadding=\"1\" id=\"CX\" style=\"display:none;\" >");
                sb.Append("<tr bgcolor=\"#CFE6FC\"> <td rowspan=\"2\" align=\"center\"> 指 标 名 称  </td>");
                sb.Append("<td rowspan=\"2\" align=\"center\">单 位</td>");
                sb.Append("<td rowspan=\"2\" align=\"center\">设 计 值</td>");
                sb.Append("<td colspan=\"1\" align=\"center\">本 期</td>");
                sb.Append("<td colspan=\"1\" align=\"center\">同 期</td></tr>");
                sb.Append("<tr bgcolor=\"#CFE6FC\">");
                sb.Append("<td align=\"center\">全厂</td>");
                sb.Append("<td align=\"center\">全厂</td></tr>");

                for (int i = 1; i < zb.Length; i++)
                {
                    sb.Append("<tr>");

                    sb.AppendFormat("<td align='center'>{0}</td>", zb[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", un[i]);

                    sb.AppendFormat("<td align='center'>{0}</td>", lay[i]);

                    if (dvExcel[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dvExcel[i].TrimEnd(','));
                    else
                        sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");

                    if (dvExcelT[i] != null)
                        sb.AppendFormat("<td align='center'>{0}</td>", dvExcelT[i].TrimEnd(','));
                    else
                        sb.AppendFormat("<td align='center'>{0}</td>", "&nbsp;");

                    sb.Append("</tr>");

                }

                sb.Append("</table>");

                #endregion
            }
            else
            { sb.Append("没有查询到结果..."); return sb.ToString(); }

            return sb.ToString();
        }

        #endregion

        public string RetQTJL(string date, string JZ, out string errMsg)
        {
            string[] jl1 = null;
            string[] jl2 = null;
            string[] jl3 = null;
            string[] val = null;
            ArrayList list1 = new ArrayList();
            ArrayList list2 = new ArrayList();

            list1 = DAL.DALEeds.RetSBJL(date, JZ, out errMsg);

            jl1 = (string[])list1[0];
            jl2 = (string[])list1[1];
            jl3 = (string[])list1[2];

            list2 = DAL.DALEeds.RetSBNM(jl3, out errMsg);

            val = (string[])list2[0];

            sb.AppendFormat("<tr style='width: 80%;' bgcolor='#CFE6FC'><td height='18' colspan='4' align='center'  style='width:10' align='center'>#{0}机</td></tr>", JZ);
            sb.Append("<tr style='width: 80%;' bgcolor='#CFE6FC'>");
            sb.Append("<td style='width: 5%;'><div align='center'/>序号</td>");
            sb.Append("<td style='width: 35%;'><div align='center'/>设备名称</td>");
            sb.Append("<td style='width: 30%;'><div align='center'/>启动时间</td>");
            sb.Append("<td style='width: 30%;'><div align='center'/>停止时间</td>");
            sb.Append("</tr>");

            if (list1.Count > 0 && list2.Count > 0)
            {
                for (int i = 0; i < val.Length; i++)
                {
                    if (val[i] != "")
                    {
                        sb.Append("<tr style='width: 80%;' bgcolor='#FFFFFF'>");
                        sb.AppendFormat("<td style='width: 5%;'><div align='center'/>{0}</td>", (i + 1).ToString());
                        sb.AppendFormat("<td style='width: 35%;'><div align='center'/>{0}</td>", val[i]);
                        sb.AppendFormat("<td style='width: 35%;'><div align='center'/>{0}</td>", jl1[i]);
                        sb.AppendFormat("<td style='width: 35%;'><div align='center'/>{0}</td>", jl2[i]);
                        sb.Append("</tr>");
                    }
                }
            }
            else
            {
                sb.Append("没有查询到结果...");
                return sb.ToString();
            }

            return sb.ToString();

        }

    }
}
